package com.ydd.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.YddAdvAddDTO;
import com.ydd.dto.YddAdvDTO;
import com.ydd.dto.YddAdvposQueryDTO;
import com.ydd.entity.YddAdvimg;
import com.ydd.entity.YddAdvpos;
import com.ydd.mapper.YddAdvimgMapper;
import com.ydd.mapper.YddAdvposMapper;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.AdvposService;
import com.ydd.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AdvposServiceImp implements AdvposService {
    @Autowired
    private YddAdvposMapper yddAdvposMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    /**
     * 新增广告位
     * @return
     */
    @Override
    public void addAdvpos(YddAdvpos yddAdvpos) {
        yddAdvposMapper.addAdvpos(yddAdvpos);
    }

    /**
     * 查询所有广告位
     *
     * @return
     */
    @Override
    public YddAdvposAllVO selectAll() {
        YddAdvposAllVO yddAdvposAllVO = yddAdvposMapper.selectAll();
        return yddAdvposAllVO;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @Override
    public void delAdvpos(String id) {
        //根据id删除广告位
        yddAdvposMapper.delAdvpos(id);
        //根据id删除广告位关联的广告图片
        yddAdvimgMapper.delAdvimg(id);
    }

    /**
     * get广告位详情
     *
     * @param id
     * @return
     */
    @Override
    public YddAdvposRowInfoVO rowInfoAdvpos(String id) {
        YddAdvposRowInfoVO yddAdvposRowInfoVO=yddAdvposMapper.rowInfoAdvpos(id);
        return yddAdvposRowInfoVO;
    }

    /**
     * 修改广告位
     *
     * @param yddAdvDTO
     * @return
     */
    @Override
    public void updateAdvpos(YddAdvDTO yddAdvDTO) {
        yddAdvposMapper.updateAdvpos(yddAdvDTO);
    }

    /**
     * 广告位动态分页查询
     *
     * @param yddAdvposQueryDTO
     * @return
     */
    @Override
    public DataListVO findByAdvpos(YddAdvposQueryDTO yddAdvposQueryDTO) {
        PageHelper.startPage(yddAdvposQueryDTO.getPage(),yddAdvposQueryDTO.getLimit());
        //多表查询复合条件的集合
        List<YddAdvimgAdvposVO> yddAdvimgAdvposVOS=yddAdvposMapper.findByAdvpos(yddAdvposQueryDTO.getAdvposdesc(),yddAdvposQueryDTO.getAdvposname());
        Page<YddAdvimgAdvposVO> pageInfo = (Page<YddAdvimgAdvposVO>) yddAdvimgAdvposVOS;
        //将数据类型转化为符合返回数据的类型
        Integer count=(int) pageInfo.getTotal();
        return new DataListVO(
                count,
                pageInfo.getPages(),
                yddAdvposQueryDTO.getLimit(),
                yddAdvposQueryDTO.getPage(),
                pageInfo.getResult()
                );
    }
}
